<?php
class site_k_root extends ss_konstrukt_masterComponent {
	protected $templates;
	protected $data;
	protected $structureMapper;
	protected $entryMapper;
	protected $strucure;
	protected $entry;
	
	public function __construct(
			ss_core_settings $ss_settings, 
			site_templatefactory $templates, 
			site_dao_structure_mapper $structureMapper, 
			site_dao_entry_mapper $entryMapper,
			ss_core_injectorInterface $injector){
		$this->structureMapper=$structureMapper;
		$this->entryMapper=$entryMapper;
		$this->structure=$structureMapper->api_master();
    	$this->templates = $templates;
		$this->settings=$ss_settings;
		parent::__construct($injector);
		//$this->injector=$injector;
		//ss_error::log($this->settings,'Settings');
	}
  
	function dispatch() {
		//ss_error::log($this->structure,'Structure');
		$this->node=$this->structure->getNodeFromSlug('/');
		$this->structureMapper->meta($this->node);
		$this->document->root($this->node);
		$this->document->addNavigation(0,$this->node);
		ss_error::log($this->next(),'Root dispatch next');
		$this->document->addHeader($this->node->header(),$this->node->header_method(),$this->node->header_recurse());
		$this->content=(is_null($this->next()) ? $this->wrap($this->forward('site_k_index')) : $this->content=parent::dispatch());
		//$this->wrap($this->forward($class_name));
		return $this->execute();
	}
  
  protected function map($name){
  	ss_error::log($name,'Mapping in root against '.$this->settings->server->appath->category);
  	switch($name){
  		case 'api';
			return 'ss_api_root';
			break;
  		case $this->settings->server->appath->entry;
			return 'site_k_entryList';
			break;
		case $this->settings->server->appath->category:
			return 'site_k_categoryList';
			break;
		default:
			if(is_string($class=$this->structure->classForSlug($name))) return $class;
			if($this->entry=$this->entryMapper->getEntryBySlug($name)->clean())return 'site_k_entry';
  	}
  }
	
	public function entry(){
		ss_error::log($this->entry,'Entry');
		return $this->entry;
	}
	/*
	protected function renderHtml() {
		ss_error::log('Root HTML render');
		//ss_error::log($this->document,'Top menu');
    	$smarty = $this->templates->create();
		if($this->content instanceof k_Response) $this->content=$this->content->toContentType('text/html');
	    $smarty->assign('content', $this->content);
	    $smarty->assign('title', $this->document->title());
	    $smarty->assign('scripts', $this->document->scripts());
	    $smarty->assign('styles', $this->document->styles());
	    $smarty->assign('onload', $this->document->onload());
	    $smarty->assign('sidebar', $this->document->sidebar());
	    $smarty->assign('topmenu', $this->document->topmenu());
	    $smarty->assign('navpath', $this->document->navigation());
	    $smarty->assign('context', $this);
	    $smarty->assign('header', $this->document->header());
    	return new k_HtmlResponse($smarty->fetch("root.tpl"));
	}
	*/
	protected function renderHtml() {
		ss_error::log('Root HTML render');
		return $this->content;
	}
	protected function wrapHtml($content){
		ss_error::log('Wrap HTML render');
		ss_error::log($this->document->topmenu(),'Top menu');
    	$smarty = $this->templates->create();
		//if($content instanceof k_Response) $content=$content->toContentType('text/html');
	    $smarty->assign('content', $content);
	    $smarty->assign('title', $this->document->title());
	    $smarty->assign('scripts', $this->document->scripts());
	    $smarty->assign('styles', $this->document->styles());
	    $smarty->assign('onload', $this->document->onload());
	    $smarty->assign('sidebar', $this->document->sidebar());
	    $smarty->assign('topmenu', $this->document->topmenu());
	    $smarty->assign('navpath', $this->document->navigation());
	    $smarty->assign('context', $this);
	    $smarty->assign('header', $this->document->header());
    	return new k_HtmlResponse($smarty->fetch("root.tpl"));
	}
	
	protected function renderJson(){
		ss_error::log('Root JSON render');
		return $this->content;
	}
	/*
	public function render(){
		return $this->wrap($this->content);
		ss_error::log('Root render');
		//if($this->content instanceof k_JsonResponse) return $this->content;
		//return parent::render();
	}
	*/
}
